package com.by.charpter03;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.by.charpter03.broadcast.BroadcastActivity;
import com.by.charpter03.services.ServiceActivity;

import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.broadcast).setOnClickListener(this);
        findViewById(R.id.service).setOnClickListener(this);
        findViewById(R.id.music).setOnClickListener(this);
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);


        tv = findViewById(R.id.msg);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.broadcast:
                startActivity(new Intent(MainActivity.this, BroadcastActivity.class));
                break;
            case R.id.service:
                startActivity(new Intent(MainActivity.this, ServiceActivity.class));
                break;
            case R.id.music:
                startActivity(new Intent(MainActivity.this, MusicActivity.class));
                break;
            case R.id.start:
                start();
                break;
            case R.id.stop:
                stop();
                break;
        }

    }

    boolean isRunning = false;
    int mims = 0;

    private void stop() {
        isRunning = false;
    }



    Timer timer = new Timer();

    public void start() {
//
//        isRunning = true;
//
//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//                while (isRunning){
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            tv.setText(mims+"");
//                        }
//                    });
//                    try {
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    mims++;
//
//                }
//
//            }
//        });
//
//        thread.start();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                mims++;
                tv.setText(mims+"");
            }
        };
        timer.schedule(timerTask,10,100);

    }




}
